package com.jcl.recruit.elasticsearch.resume.logic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jcl.core.common.page.PageResult;
import com.jcl.recruit.elasticsearch.core.builds.ElasticConditionBuild;
import com.jcl.recruit.elasticsearch.core.builds.ElasticSearchBuild;
import com.jcl.recruit.elasticsearch.core.common.ElasticResultMd;
import com.jcl.recruit.elasticsearch.core.entity.ElasticsearchEntity;
import com.jcl.recruit.elasticsearch.core.entity.vo.SearchResumeRecord;
import com.jcl.recruit.elasticsearch.core.tools.Condition;
import com.jcl.recruit.elasticsearch.core.utils.ElasticConfigUtils;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClassName RecruitElasticLogic
 * @Author yyliu
 * @Date 2018/11/19 11:26
 * @Version 1.0
 **/
@Component
public class RecruitElasticLogic {

    private static final Logger logger = LoggerFactory.getLogger(RecruitElasticLogic.class);

    @Autowired
    private ElasticSearchBuild elasticSearchBuild;

    /**
     * @MethodName searchResumePage
     * //TODO
     * @Param [recordList, channel, curPage, elasticResultMd]
     * @Return com.jcl.recruit.core.common.page.PageResult
     * @Author 63404
     * @Date 2018/11/2
     **/
    public PageResult searchResumePage(List<SearchResumeRecord> recordList,
                                       String channel,
                                       String corpCode,
                                       Integer curPage,
                                       Integer resumeUpdateDays,
                                       Integer comingDays,
                                       ElasticResultMd elasticResultMd) {
        Condition condition = ElasticConditionBuild.conditionSearchResumeJson(recordList, channel, corpCode, curPage,
                resumeUpdateDays, comingDays);
        String jsonParamStr = JSONObject.toJSONString(condition);
        ElasticsearchEntity elasticsearchEntity = elasticSearchBuild.searchResume(jsonParamStr);
        PageResult<Map<String, Object>> pageResult = ElasticConfigUtils.searchPageResult(elasticsearchEntity, curPage);
        List<Map<String, Object>> dataList = pageResult.getData();
        if (null != elasticResultMd) {
            dataList = elasticResultMd.callback(dataList);
            pageResult.setData(dataList);
        }
        return pageResult;
    }

    public String addSearchResume(Map<String, Object> dataMap) {
        try {
            String id = dataMap.get("id").toString();
            String jsonParam = JSON.toJSONString(dataMap);
            String result = elasticSearchBuild.createSearchResume(id, jsonParam);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public String catApi() throws Exception {
        HttpEntity entity = elasticSearchBuild.catApi();
        String result = EntityUtils.toString(entity);
        System.out.println(result);
        return result;
    }

    public String queryAll() throws Exception {
        HttpEntity entity = elasticSearchBuild.queryAll();
        String result = EntityUtils.toString(entity);
        System.out.println(result);
        return result;
    }
}
